package com.homework;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @description:
 * @author: jhy
 * @time: 2021/4/25 20:43
 */
public class PrivateTestDemo1 {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {

        //1.拿到PrivateTest的类实例对象
        Class<?> cls = Class.forName("com.homework.PrivateTest");

        //2.拿到实例对象
        PrivateTest o = (PrivateTest)cls.newInstance();

        //3.通过getDeclaredField(字段名)可以得到任意访问权限的字段
        Field name = cls.getDeclaredField("name");

        //4.对name进行爆破---->将其设为可访问
        name.setAccessible(true);

        //5.设置name的值为"进大厂"
        name.set(o,"进大厂");

        //6.通过getMethod("方法名")可以得到public的指定方法
        Method getName = cls.getMethod("getName");

        //7.通过反射调用getName方法
        System.out.println("name :" + getName.invoke(o));

    }
}
